package com.fdb.efp.nls.service.impl.ctrloancont.ctr;

import com.fdb.efp.nls.service.vo.NlsApplyInfoVO;
import com.fdb.basic.framework.core.constant.SDicCertTypeEnum;
import com.fdb.basic.framework.core.exception.BizException;
import com.fdb.basic.framework.core.util.Amount2RMB;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.efp.nls.service.facade.PrdInfoLprService;
import com.fdb.efp.nls.service.vo.PrdInfoLprVO;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 需求编号:2019D08--
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2020/3/13
 * 功能描述:
 * 修改日期:2020/3/13
 * 修改描述:
 */
@Component("psdApplyCtrLoanCont")
public class PsdApplyCtrLoanCtr extends AbstractCtrLoanCtrBean<NlsApplyInfoVO> {
    //@Value("") 输入路径
    //@Value("") 输入路径
    // 还款方式
    protected String repayMethod;
    // LPR利率
    protected String LPRRate;

    public PsdApplyCtrLoanCtr() {
        // 输入路径
        inputPath = "";
        // 输出路径
        outputPath = "";
    }

    @Override
    protected void setSpecifiedLoanContMedaData(NlsApplyInfoVO nlsApplyInfoVO) throws BizException {
        CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
        ctrLoanContVO.setContNo(nlsApplyInfoVO.getLmtApplySeq());
        ctrLoanContVO = ctrLoanContService.queryByPk(ctrLoanContVO);
        if (Objects.nonNull(ctrLoanContVO)) {
            //客户姓名
            addContMetaData("cusName", ctrLoanContVO.getCusName());
            //证件类型
            addContMetaData("certType", SDicCertTypeEnum.getDesc(ctrLoanContVO.getCertType()));
            //证件号码
            addContMetaData("certCode", ctrLoanContVO.getCertCode());
            //客户住址
            addContMetaData("cusAddr", ctrLoanContVO.getResdntAddr());
            //客户邮编
            addContMetaData("cusZipcode", ctrLoanContVO.getPstcd());
            //客户传真
            addContMetaData("cusTax", ctrLoanContVO.getClnttFax());
            //客户电话
            addContMetaData("cusPhone", ctrLoanContVO.getCtcTelNo());
            //客户电子邮箱
            addContMetaData("cusEmail", ctrLoanContVO.getEmail());
        }
        String amount = nlsApplyInfoVO.getApplyAmt().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        addContMetaData("lmtAmtLower", amount);
        addContMetaData("lmtAmtUpper", Amount2RMB.convert(amount));
        addContMetaData("contNo", nlsApplyInfoVO.getApplySeq());
        addContMetaData("repayMethod", "101".equals(nlsApplyInfoVO.getRepayMethod()) ? "1" : "2");
        PrdInfoLprVO prdInfoLprVO = prdInfoLprService.queryByIntratecdAndTerm(new PrdInfoLprVO());
        // (固定利率 - 当月LPR利率）* 10000
        if (prdInfoLprVO != null) {
            BigDecimal loanRate = nlsApplyInfoVO.getLoanRate().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);
            String lprRate = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).abs().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            String quoteRates = prdInfoLprVO.getIntrate().setScale(2, BigDecimal.ROUND_HALF_UP).toString()+"%";
            addContMetaData("quoteRates",quoteRates);
            addContMetaData("LRPR", lprRate);
            String operator = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).signum()==1?"加":"减";
            addContMetaData("operator",operator);
        }
        //签约日期 yyyy-MM-dd
        addContMetaData("signDate", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
        Date currDate = new Date(System.currentTimeMillis());
        String loanTermType = nlsApplyInfoVO.getLoanTermType();
        String loanTerm = nlsApplyInfoVO.getLoanTerm();
        // 当前年月日
        addContMetaData("loanStartY",String.valueOf(DateTool.getYear(currDate)));
        addContMetaData("loanStartM",String.valueOf(DateTool.getMonth(currDate)));
        addContMetaData("loanStartD",String.valueOf(DateTool.getDay(currDate)));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currDate);
        int term = Integer.parseInt(nlsApplyInfoVO.getLoanTerm());
        if ("Y".equals(nlsApplyInfoVO.getLoanTermType())){
            calendar.add(Calendar.YEAR,term);
        }else if ("M".equals(nlsApplyInfoVO.getLoanTermType())){
            calendar.add(Calendar.MONTH,term);
        }else if ("D".equals(nlsApplyInfoVO.getLoanTermType())){
            calendar.add(Calendar.DAY_OF_YEAR,term);
        }
        Date endDate = calendar.getTime();
        addContMetaData("loanEndY", String.valueOf(DateTool.getYear(endDate)));
        addContMetaData("loanEndM", String.valueOf(DateTool.getMonth(endDate)));
        addContMetaData("loanEndD", String.valueOf(DateTool.getDay(endDate)));
    }
    @Autowired
    @Qualifier("ctrLoanContService")
    private CtrLoanContService ctrLoanContService;
    @Autowired
    @Qualifier("prdInfoLprService")
    private PrdInfoLprService prdInfoLprService;
}
